//
//  reviseSecretVC.swift
//  DingDongZP
//
//  Created by Zhongnan on 16/4/10.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

class reviseSecretVC: LDBaseCustomizeVC {

    var oldSecret:baseTextFiled?
    var newSecret:baseTextFiled?
    var confirmSecret:baseTextFiled?
    
    var confirmBtn:UIButton?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setDefaultContent()
        
    }
    
    func sendReq() {
        
        guard oldSecret?.text?.characters.count > 0 else {
            showLDAlertView("请输入原密码")
            return
        }
        guard (newSecret?.text?.characters.count > 0) || (confirmSecret!.text!.characters.count > 0) else {
            showLDAlertView("确认密码不能为空")
            return
        }
        guard newSecret?.text == confirmSecret!.text else{
            showLDAlertView("确认密码不一致")
            return
        }
        guard newSecret?.text != oldSecret?.text else {
            showLDAlertView("修改密码和原密码一致")
            return
        }
        
        
        var dictInfo = [String:String]()
        dictInfo["password"] = (oldSecret?.text)! as String ?? "NULL"
        dictInfo["newpassword"] = (newSecret?.text)! as String ?? "NULL"
        
        
        
        LDShowHUB.shareInstance.showStaus("更改中")
        LDUserInfoNet.sharedInstance.changeJobfinderPassWord(dictInfo, success: { [weak self] (data) in
            LDShowHUB.shareInstance.showSucc("修改成功")
            // 保存token
            let newHasPas = data
            DDLoginUserManager.setToken(newHasPas)
            self?.navigationController?.popViewControllerAnimated(true)
            
        }) { (data) in
            LDShowHUB.shareInstance.showErr(data)
        }
    }
    
    func setDefaultContent() {
        
        view.backgroundColor = LDBackGoundColorGray
        oldSecret = UITextField.setTextFileldText("原密码", placeHold: "请输入原密码")
        newSecret = UITextField.setTextFileldText("新密码", placeHold: "请设置登录密码")
        confirmSecret = UITextField.setTextFileldText("确认密码", placeHold: "请再次输入密码")
        confirmBtn = UIButton.setDetailsBtn("修改密码", picName: nil, btnBackColor: LDGolbalColor.Green, titleColor: LDFontColor.White)
        confirmBtn?.titleLabel?.font = LDFont.Font16
        confirmBtn?.layer.cornerRadius = 3
        confirmBtn?.bk_whenTapped({ [weak self] in
            self?.sendReq()
        })
        
        view.addSubview(oldSecret!)
        view.addSubview(newSecret!)
        view.addSubview(confirmSecret!)
        view.addSubview(confirmBtn!)
        oldSecret?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(LDMargin_V*4)
            make.left.right.equalTo(0)
            make.height.equalTo(50)
        })
        newSecret?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo((oldSecret?.snp_bottom)!).offset(LDMargin_V*4)
            make.left.right.equalTo(0)
            make.height.equalTo(50)
        })
        confirmSecret?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo((newSecret?.snp_bottom)!)
            make.left.right.equalTo(0)
            make.height.equalTo(50)
        })
        confirmBtn?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo((confirmSecret?.snp_bottom)!).offset(LDMargin_V*4)
            make.left.equalTo(LDMargin_V*2)
            make.right.equalTo(-LDMargin_V*2)
            make.height.equalTo(50)
        })
    }
}
